#!/bin/bash
#\
	exec tclsh "$0" "$@"
package req Expect
package req log

# $Id$
# Usage:
#./main.tcl -llog.xml -tcase_001 -ccase_001/cmd -scase_001/ixia.tcl -rcase_001/clean
source include/global.tcl
source include/common_lib.tcl

# initialize variables
set cmd_file 		""
set exec_script 	""
set log_flag		0
set case_title_flag 	0
set script_file_flag	0
set cmd_file_flag 	0
set restore_file_flag 	0 

# process options
set end_opt_index -1

foreach arg $argv { 
   	if {![string match "-\[a-zA-Z]*" $arg]} {
        	break 
    	}	

    	# inc end option index
    	incr end_opt_index

    	# get option flag and option value
    	set optFlg [string range $arg 1 1]
    	set optval [string range $arg 2 end]
    	debug "$optFlg $optval"
    
	if {$optval == ""} {
        	debug "option value is null: -$optFlg"
        	return -1
    	}

    	switch $optFlg {
		"u" { # username, defualt is root
			if { $optval != ""} {
				set user $optval
				debug "user: $user"
				global g_user
				exec sed -i "s/$g_user/$user/g" global.tcl	
				source include/common_lib.tcl
			} else {
				puts "Please specify username you want to use"
				return -1
			}
		}
		"p" { # password, default is embed220
			if { $optval != ""} {
				set passwd $optval
				debug "password: $passwd"
				global g_passwd 
				exec sed -i "s/$g_passwd/$passwd/g" global.tcl
				source include/common_lib.tcl
			} else {
				puts "Please specify password you want to use"
				return -1
			}
		}
		"i" { # device ip, you need to change this variable in global.tcl.
			if { $optval != ""} {
				set devip $optval
				debug "devip: $devip"
				set g_devip $devip
				#global g_devip
				#exec sed -i "s/$g_devip/$devip/g" global.tcl
				#source include/common_lib.tcl
			} else {
				puts "Please specify device IP you want to use"
				return -1
			}
		}
		"c" {	# test environnement setup command
			set cmd_file $optval
			if {$cmd_file == ""} {
				puts "Please specify cmd_file name with -c option"
				return -1
			}            	
			set cmd_file_flag 1
			debug "cmd file: $cmd_file"
		}
		"s" {	# ixia tcl script	
			set exec_script $optval
			if {$exec_script == ""} {
				puts "Please specify script. name using -s option"
				return -1
			}		
			set script_file_flag 1
			debug "IXIA script: $exec_script"
		}
		"r" {	# test environnement restore command
			set restore_file $optval
			if {$restore_file == ""} {
				puts "Please specify restore_file with -r option"
				return -1 
			}
			set restore_file_flag 1
			debug "test environnement restore command: $restore_file"
		}	
		"l" {	# log file, default is log.xml
			set log_file $optval
			if {$log_file == ""} {
				puts "Please specify log_file with -l option"
				return -1
			}		
			set log_flag 1
			debug "log file: $log_file"
		}
		"t" {	#case title
			set case_title $optval
			if {$optval == ""} {
				puts "Please specify case_title with -C option"
				return -1
			}		
			set case_title_flag 1
			debug "case title: $case_title"
		}
		default {
		    puts "unknown option: -$optFlg"
		    return -1
        }
    } ;# end switch
} ;# end foreach

if  {$log_flag == 0} {	# if not specify the log file, use log.xml default.
	#set log_file log.xml	#for xmllint 
	if [ file exists log.xml ] { 
		if [catch {open log.xml {RDWR APPEND}} log_fd] {
			puts "cannot open log.xml with {RDWR APPEND} !"
			exit -1
		}
	} else {
		if [catch {open log.xml {RDWR CREAT}} log_fd] {
			puts "cannot open log.xml with {RDWR CREAT} !"
			exit -1
		}
		puts $log_fd "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
	}	
	
} else { 
	if [ file exists $log_file ] {
		if [catch {open $log_file {RDWR APPEND}} log_fd] {
			puts "cannot open $log_file with {RDWR APPEND} !"
			exit -1
		}
	} else {
		if [catch {open $log_file {RDWR CREAT}} log_fd] {
			puts "cannnot open $log_file with {RDWR CREAT} !"
			exit -1
		}
		puts $log_fd "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
	}
}

if {$case_title_flag} {
	puts $log_fd "<case>"
	puts $log_fd "\t<title>$case_title</title>"
}
if {$cmd_file_flag} {
	puts $log_fd "\t<exec_cmd>"
	puts $log_fd "\t\t<file>$cmd_file</file>"
	exec_cmd_file_with_ssh $cmd_file $log_fd
	puts $log_fd "\t</exec_cmd>"
}
if {$script_file_flag} {
	puts $log_fd "\t<script>"
	puts $log_fd "\t\t<file>$exec_script</file>"
	source $exec_script
	set dir [pwd]	
	set transmit_log [exec cat $dir/ixia.log]
	puts $log_fd "\t\t<transmit>"
	puts $log_fd "$transmit_log"
	puts $log_fd "\t\t</transmit>"
	puts $log_fd "\t\t<result>"
	puts $log_fd ""
	puts $log_fd "\t\t</result>"
	puts $log_fd "\t</script>"
}
if {$restore_file_flag} {
	puts $log_fd "\t<restore_cmd>"
	puts $log_fd "\t\t<file>$restore_file</file>"
	exec_cmd_file_with_ssh $restore_file $log_fd
	puts $log_fd "\t</restore_cmd>"
}
puts $log_fd "</case>\n"
#xmllint -formart $log_file
close $log_fd


set counter {
[Port: A1]
        Rx Good Pkts                        3975052 pkts                 0 pps
        Rx Bytes                         5406070720 bytes                0 kbps
        Rx Oversize Frame                         0 pkts                 0 pps
        Rx Alignment Error                        0 pkts                 0 pps
        Rx FCS Error                              0 pkts                 0 pps
        Rx Fragment                               0 pkts                 0 pps}
	